package com.unitd.base.module.api.api;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unitd.base.frame.plugin.module.dto.ResponseUtils;
import com.unitd.base.frame.util.ListUtils;
import com.unitd.base.module.comm.cache.RegionCacheUtil;
import com.unitd.base.module.comm.utils.StringUtils;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.pojo.dto.RegionVO;
import com.unitd.base.module.master.pojo.po.RegionPO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lipengjun
 * @email 939961241@qq.com
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 */
@Api(tags = "评论")
@RestController
@RequestMapping("/api/region")
public class ApiRegionController extends ApiBaseAction {

    @ApiOperation(value = "地区列表")
    @IgnoreAuth
    @PostMapping("list")
    public Object list(Long parentId) {
        // 先得到缓存下指定父节点下的所有有效行政地区信息
        List<RegionPO> regions = RegionCacheUtil.getChildrenByParentId(parentId);

        // 数据转换
        return convertPo2Vo(regions);
    }

    @IgnoreAuth
    @PostMapping("provinceList")
    public Object provinceList() {
        List<RegionPO> regions = RegionCacheUtil.getAllProvice();

        // 数据转换
        return convertPo2Vo(regions);
    }


    @IgnoreAuth
    @PostMapping("cityList")
    public Object provinceList(String proviceName) {
        List<RegionPO> regions = RegionCacheUtil.getChildrenCity(proviceName);

        // 数据转换
        return convertPo2Vo(regions);
    }

    @IgnoreAuth
    @PostMapping("distinctList")
    public Object distinctList(String proviceName, String cityName) {
        List<RegionPO> regions = RegionCacheUtil.getChildrenDistrict(proviceName, cityName);

        // 数据转换
        return convertPo2Vo(regions);
    }


    @IgnoreAuth
    @PostMapping("info")
    public Object info(Long regionId) {
        RegionPO region = RegionCacheUtil.getAreaByAreaId(regionId);
        return ResponseUtils.toResponsSuccess(new RegionVO(region));
    }

    @IgnoreAuth
    @PostMapping("regionIdsByNames")
    public Object regionIdsByNames(String provinceName, String cityName, String districtName) {
        Map<String, Long> resultObj = Maps.newHashMap();
        Long provinceId = 0L;
        Long cityId = 0L;
        Long districtId = 0L;
        if (null != provinceName) {
            provinceId = RegionCacheUtil.getProvinceIdByName(provinceName);
        }
        if (null != provinceId && !StringUtils.isNullOrEmpty(cityName)) {
            cityId = RegionCacheUtil.getCityIdByName(provinceId, cityName);
        }
        if (null != provinceId && null != cityId && !StringUtils.isNullOrEmpty(districtName)) {
            districtId = RegionCacheUtil.getDistrictIdByName(provinceId, cityId, districtName);
        }
        resultObj.put("provinceId", provinceId);
        resultObj.put("cityId", cityId);
        resultObj.put("districtId", districtId);
        return ResponseUtils.toResponsSuccess(resultObj);
    }

    /**
     * 将po对象转换成vo对象
     *
     * @param regions po对象列表
     * @return 反馈结果
     */
    private Map<String, Object> convertPo2Vo(List<RegionPO> regions) {
        // 数据转换
        List<RegionVO> regionVoList = Lists.newArrayList();
        if (!ListUtils.isNull(regions)) {
            regionVoList = regions.stream().map(RegionVO::new).collect(Collectors.toList());
        }
        return ResponseUtils.toResponsSuccess(regionVoList);
    }
}
